/**
 * Created with JetBrains WebStorm.
 * User: Brose
 * Date: 2015/5/8
 * Time: 13:49
 */
/**
 *
 * @type {string}
 */
require(
    [
        'echarts',
        'echarts/chart/gauge',
        'echarts/chart/pie'
    ],
    function (ec) {
        initSrvCpu(ec);
        initSrvMemory(ec);
        initSrvDisk(ec);
    }
);

var cpuChart, memoryChart, diskChart;
var _domSrvCpu = document.getElementById("chart-cpu");
_domSrvMemory = document.getElementById("chart-memory");
_domSrvDisk = document.getElementById("chart-disk");
var _themeName = 'macarons', //主题风格//主题风格 
    _animationEasing = '', //主元素动画效果
    _backgroundColor = '#fff',
    _axisPointer = {}, //坐标轴样式
    _tooltip = {},
    _toolbox = { //工具栏
        // show:true,
        orien: 'vertical',
        y: "center",
        feature: {
            restore: {
                show: true
            },
            saveAsImage: {
                show: true
            }
        }
    },
    _calculable = true, //拖拽重计算默认为false
    _symbolSize = 2,
    _grid = {
        x: 45,
        y: 10,
        x2: 15,
        y2: 65
    },
    _curIndx = 0,
    _legendMax1135 = { //分辨率<1255
        orient: 'horizontal',
        x: 'center',
        y: 15,
        itemGap: 10,
        itemWidth: 15,
        padding: 0
    },
    _legendMax1366 = { //分辨率<=1366
        orient: 'horizontal',
        x: 'center',
        y: 15,
        itemGap: 15,
        padding: 15

    };


/**
 * 初始化服务器资源三个图表
 * initSrcCpu,initSrvMemory,initSrvDisk
 *
 **/
var labelTop = {
    normal: {
        label: {
            formatter: function (a) {
                return a.name + "\n" + (a.value*1).toFixed(2) + '%'
            },
            position: 'center',
            textStyle: {
                fontSize: '14'
            }
        },
        labelLine: {
            show: false
        }
    }
};
var labelFromatter = {
    normal: {
        label: {
            formatter: function (a) {
                return (100 - a.value).toFixed(2) + '%'
            },
            textStyle: {
                baseline: 'top',
                fontSize: '14',
                fontWeight: 'bold'
            }
        }
    },
}
var labelBottom = {
    normal: {
        label: {
            show: false
        },
        labelLine: {
            show: false
        }
    },
    emphasis: {}
};
var _srvToolTip = {
    trigger: 'item',
    formatter: "{b}:({d}%)"
};
var _radius = ["70%", "90%"];
var _color = ['#99d2dd', '#1c7099'];
var _speed=300000;
function initSrvCpu(ec) {
    cpuChart = ec.init(_domSrvCpu, _themeName);
    var _requests=["./media/src.json",jsonUrl + "sysInfo/cpu"]
    $.myAjax(cpuChart,_requests[_REQUEST_TYPE], {}, function (data) {
        var option = {
            backgroundColor: _backgroundColor,
            color: _color,
            tooltip: _srvToolTip,
            series: [{
                name: 'cpu使用率',
                type: 'gauge',
                center: ['50%', '50%'], // 默认全局居中
                radius: "90%",
                startAngle: 125,
                endAngle: -125,
                min: 0, // 最小值
                max: 100, // 最大值
                precision: 0, // 小数精度，默认为0，无小数点
                splitNumber: 10, // 分割段数，默认为5
                axisLine: { // 坐标轴线
                    show: true, // 默认显示，属性show控制显示与否
                    lineStyle: { // 属性lineStyle控制线条样式
                        color: [
                            [0.2, '#99d2dd'],
                            [0.4, '#88b0bb'],
                            [0.8, '#1bb2d8'],
                            [1, '#1790cf']
                        ],
                        width: 8
                    }
                },
                axisTick: { // 坐标轴小标记
                    show: true, // 属性show控制显示与否，默认不显示
                    splitNumber: 5, // 每份split细分多少段
                    length: 8, // 属性length控制线长
                    lineStyle: { // 属性lineStyle控制线条样式
                        color: '#eee',
                        width: 1,
                        type: 'solid'
                    }
                },
                axisLabel: { // 坐标轴文本标签，详见axis.axisLabel
                    show: true,
                    formatter: function (v) {
                        switch (v + '') {
                            case '10':
                                return '弱';
                            case '30':
                                return '低';
                            case '60':
                                return '中';
                            case '90':
                                return '高';
                            default:
                                return '';
                        }
                    },
                    textStyle: { // 其余属性默认使用全局文本样式，详见TEXTSTYLE
                        color: '#333',
                        fontSize: 8
                    }
                },
                splitLine: { // 分隔线
                    show: true, // 默认显示，属性show控制显示与否
                    length: 13, // 属性length控制线长
                    lineStyle: { // 属性lineStyle（详见lineStyle）控制线条样式
                        color: '#eee',
                        width: 2,
                        type: 'solid'
                    }
                },
                pointer: {
                    length: '80%',
                    width: 5,
                    color: '#1790cf'
                },
                title: {
                    show: true,
                    offsetCenter: ['-100%', -5], // x, y，单位px
                    textStyle: { // 其余属性默认使用全局文本样式，详见TEXTSTYLE
                        color: '#333',
                        fontSize: 10
                    }
                },
                detail: {
                    show: true,
                    backgroundColor: 'rgba(0,0,0,0)',
                    borderWidth: 0,
                    borderColor: '#ccc',
                    width: 0,
                    height: 0,
                    offsetCenter: ['-85%', 10], // x, y，单位px
                    formatter: '{value}%',
                    textStyle: { // 其余属性默认使用全局文本样式，详见TEXTSTYLE
                        color: 'auto',
                        fontSize: 12
                    }
                },
                data: [{
                    value: data.resultDatas.seriesData.src_use,
                    name: 'CPU使用率'
                }]
            }],
            animation:false
        };
        cpuChart._mySetOption(option);
        clearInterval(t1);
        var t1 = setInterval(function () {
            $.ajax({
                url: _requests[_REQUEST_TYPE],
                data: {},
                type: "GET",
                dataType: "jsonp",
                jsonp: "callback",
                success: function (data) {
                    var _src_use = data.result.resultDatas.seriesData.src_use;
                    option.series[0].data[0].value = _src_use * 1;
                    option.animation=true;
                    cpuChart.setOption(option, true);
                }, error: function (er) {
                    option.series[0].data[0].value = (Math.random() * 100).toFixed(2) - 0;
                    option.animation=true;
                    cpuChart.setOption(option, true);
                }
            })
        }, _speed);
    });
}

function initSrvMemory(ec) {
    memoryChart = ec.init(_domSrvMemory, _themeName);
    var _requests=["./media/src.json",jsonUrl + "sysInfo/cpu"]
    $.myAjax(memoryChart, _requests[_REQUEST_TYPE], {}, function (data) {
        var option = {
            backgroundColor: _backgroundColor,
            color: _color,
            tooltip: _srvToolTip,
            calculable: false,
            series: [{
                name: '磁盘使用率',
                type: 'pie',
                radius: _radius,

                data: [{
                    value: (100 - data.resultDatas.seriesData.src_use),
                    name: '内存空闲',
                    itemStyle: labelBottom
                }, {
                    value: data.resultDatas.seriesData.src_use,
                    name: '内存使用',
                    itemStyle: labelTop
                }
                ]
            }],
            animation:false
        };
        memoryChart._mySetOption(option);
        memoryChart.refresh();
        clearInterval(t2);
        var t2 = setInterval(function () {
            $.ajax({
                url: _requests[_REQUEST_TYPE],
                data: {},
                type: "GET",
                dataType: "jsonp",
                jsonp: "callback",
                success: function (data) {
                    var _src_use = data.result.resultDatas.seriesData.src_use,
                        _src_free = 100-_src_use;
                    option.series[0].data[0].value = _src_free*1;
                    option.series[1].data[0].value = _src_use*1;
                    option.animation=true;
                    memoryChart.setOption(option, true);
                }, error: function (er) {
                    var k = (Math.random() * 100).toFixed(2),
                        s = 100 - k;
                    option.series[0].data[0].value = k - 0;
                    option.series[0].data[1].value = s - 0;
                    option.animation=true;
                    memoryChart.setOption(option, true);
                }
            })
        }, _speed);
    });
}

function initSrvDisk(ec) {
    diskChart = ec.init(_domSrvDisk, _themeName);
    var _requests=["./media/src.json",jsonUrl + "sysInfo/cpu"]
    $.myAjax(diskChart, _requests[_REQUEST_TYPE], {}, function (data) {
        var option = {
            backgroundColor: _backgroundColor,
            color: _color,
            tooltip: _srvToolTip,
            calculable: false,
            series: [{
                name: '磁盘使用率',
                type: 'pie',
                radius: _radius,
                data: [{
                    value: (100 - data.resultDatas.seriesData.src_use),
                    name: '磁盘空闲',
                    itemStyle: labelBottom
                }, {
                    value: data.resultDatas.seriesData.src_use,
                    name: '磁盘使用',
                    itemStyle: labelTop
                }
                ]
            }],
            animation:false
        };
        diskChart._mySetOption(option);
        diskChart.refresh();
        clearInterval(t3);
        var t3 = setInterval(function () {
            $.ajax({
                url: _requests[_REQUEST_TYPE],
                data: {},
                type: "GET",
                dataType: "jsonp",
                jsonp: "callback",
                success: function (data) {
                    var _src_use = data.result.resultDatas.seriesData.src_use,
                        _src_free = 100-_src_use;
                    option.series[0].data[0].value = _src_free*1;
                    option.series[1].data[0].value = _src_use*1;
                    option.animation=true;
                    diskChart.setOption(option, true);
                }, error: function (er) {
                    var k = (Math.random() * 100).toFixed(2),
                        s = 100 - k;
                    option.series[0].data[0].value = k - 0;
                    option.series[0].data[1].value = s - 0;
                    option.animation=true;
                    diskChart.setOption(option, true);
                }
            })

        }, _speed);
    });

}
